package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.SmsChannelForm;
import com.dimples.dd.system.model.query.SmsChannelPageQuery;
import com.dimples.dd.system.model.vo.SmsChannelPageVO;
import com.dimples.dd.system.service.SystemSmsChannelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.simpleframework.xml.core.Validate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/10/28
 */
@Validate
@Tag(name = "短信管理-短信渠道")
@RestController
@RequestMapping("sms-channel")
@RequiredArgsConstructor
public class SysSmsChannelController {

    private final SystemSmsChannelService systemSmsChannelService;

    @PostMapping
    @Operation(summary = "创建短信渠道")
    @PreAuthorize("@ss.hasPerm('system:sms-channel:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Long> createSmsChannel(@Valid @RequestBody SmsChannelForm createSmsChannelForm) {
        return CommonResult.success(this.systemSmsChannelService.createSmsChannel(createSmsChannelForm));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新短信渠道")
    @PreAuthorize("@ss.hasPerm('system:sms-channel:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateSmsChannel(
            @PathVariable Long id,
            @Valid @RequestBody SmsChannelForm updateSmsChannelForm
    ) {
        updateSmsChannelForm.setId(id);
        this.systemSmsChannelService.updateSmsChannel(updateSmsChannelForm);
        return CommonResult.success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除短信渠道")
    @PreAuthorize("@ss.hasPerm('system:sms-channel:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteSmsChannel(
            @Parameter(name = "id", description = "编号", required = true) @PathVariable Long id
    ) {
        this.systemSmsChannelService.deleteSmsChannel(id);
        return CommonResult.success(true);
    }

    @GetMapping("/{id}/form")
    @Operation(summary = "短信渠道表单数据")
    @PreAuthorize("@ss.hasPerm('system:sms-channel:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<SmsChannelForm> selectSmsChannel(
            @Parameter(name = "id", description = "编号", required = true, example = "1024") @PathVariable Long id
    ) {
        SmsChannelForm channel = this.systemSmsChannelService.selectSmsChannel(id);
        return CommonResult.success(channel);
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信渠道分页")
    @PreAuthorize("@ss.hasPerm('system:sms-channel:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public PageResult<SmsChannelPageVO> selectSmsChannelPage(@Valid SmsChannelPageQuery pageQuery) {
        return this.systemSmsChannelService.selectSmsChannelPage(pageQuery);
    }

    @GetMapping({"/options"})
    @Operation(summary = "获得短信渠道精简列表", description = "包含被禁用的短信渠道")
    public CommonResult<List<Option<Long>>> listSmsChannelOptions() {
        List<Option<Long>> list = this.systemSmsChannelService.listSmsChannelOptions();
        return CommonResult.success(list);
    }


}
